package com.hjq.mallmember.service.impl;

import com.hjq.common.utils.PasswordCodingUtils;
import com.hjq.mallmember.dao.MemberLevelDao;
import com.hjq.mallmember.entity.vo.UserLoginVO;
import com.hjq.mallmember.entity.vo.UserRegVO;
import com.hjq.mallmember.exception.phoneExistException;
import com.hjq.mallmember.exception.userNameExistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.Query;

import com.hjq.mallmember.dao.MemberDao;
import com.hjq.mallmember.entity.MemberEntity;
import com.hjq.mallmember.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    MemberLevelDao levelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void registerNewMember(UserRegVO regVO) throws phoneExistException, userNameExistException {
        MemberEntity memberEntity = new MemberEntity();
        Long defaultLevelStatusId = levelDao.getDefaultLevel().getId();
        memberEntity.setLevelId(defaultLevelStatusId);
        //如果电话号码存在，抛异常
        if (checkPhoneNumUnique(regVO.getPhoneNum())) {
            throw new phoneExistException();
        }
        ;
        //如果用户名存在，抛异常
        if (checkUserNameUnique(regVO.getUsername())) {
            throw new userNameExistException();
        }


        memberEntity.setUsername(regVO.getUsername());
        memberEntity.setNickname(regVO.getUsername());
        memberEntity.setMobile(regVO.getPhoneNum());
        String encoded = PasswordCodingUtils.encode(regVO.getPassword());
        memberEntity.setPassword(encoded);


        baseMapper.insert(memberEntity);

    }

    @Override
    public boolean checkPhoneNumUnique(String phoneNum) {
        Integer integer = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phoneNum));
        return integer > 0;
    }

    @Override
    public boolean checkUserNameUnique(String username) {
        Integer integer = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
        return integer > 0;
    }

    @Override
    public MemberEntity login(UserLoginVO loginVO) {
        //MemberEntity memberEntity = new MemberEntity();
        String account = loginVO.getUserAccount();
        String password = loginVO.getPassword();
        MemberEntity loginAccount = baseMapper.getLoginAccount(account);
        if(loginAccount==null){
            return null;
        }
        if(!PasswordCodingUtils.isMatch(password,loginAccount.getPassword())){
            return  null;
        }
        return loginAccount;
    }

}